<template>
  <div>
    <div class="card" style="margin-bottom: 5px">
      <el-input clearable @clear="load" style="width: 260px;margin-right: 5px"  v-model="data.title" placeholder="请输入板块名称查询" :prefix-icon="Search"></el-input>
      <el-button type="primary" @click="load">查询</el-button>
      <el-button @click="reset">重置</el-button>
    </div>

    <div class="card" style="margin-bottom: 5px" v-if="data.user.role==='ADMIN'">
      <el-button type="danger" @click="delBatch">批量删除</el-button>
      <el-button type="primary" @click="handleAdd">新 增</el-button>
    </div>

    <div class="card" style="margin-bottom: 5px">
      <el-table :data="data.tableData" style="width: 100%" :header-cell-style="{color:'#333',backgroundColor:'#eaf4ff'}" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="title" label="分类名称"/>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button type="primary" icon="Edit" circle @click="handleEdit(scope.row)"></el-button>
            <el-button type="danger" icon="Delete" circle @click="del(scope.row.id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="card" style="margin-bottom: 5px">
      <el-pagination
          v-model:current-page="data.pageNum"
          v-model:page-size="data.pageSize"
          :page-sizes="[5,10,20]"
          :total="data.total"
          layout="total,sizes, prev, pager, next,jumper"
          @current-change="load"
          @size-change="load"
      />
    </div>

    <el-dialog title="社区板块" v-model="data.formVisible"  width="500" destroy-on-close>
      <el-form ref="formRef" :model="data.form" :rules="data.rules" label-width="80px" style="padding: 20px 30px 10px 0">
        <el-form-item prop="title" label="板块名称" >
          <el-input v-model="data.form.title" autocomplete="off" placeholder="请输入分类标题"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="data.formVisible = false">取消</el-button>
          <el-button type="primary" @click="save">保存</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {Search} from "@element-plus/icons-vue";
import {reactive,ref} from "vue";
import request from "@/utils/request.js";
import {ElMessage, ElMessageBox} from "element-plus";

const formRef=ref()

const data=reactive({
  user:JSON.parse(localStorage.getItem('code_user')||"{}"),
  title:null,
  pageNum:1,
  pageSize:5,
  total:0,
  tableData:[],
  ids:[],
  form:{},
  formVisible:false,
  rules:{
    title:[
      {required:true,message:'请填写分类标题',trigger:'blur'},
    ]
  }
})

const load =()=>{
  request.get('/postsSection/selectPage',{
    params:{
      pageNum:data.pageNum,
      pageSize:data.pageSize,
      title:data.title
    }
  }).then(res=>{
    if(res.code==='200'){
      data.tableData=res.data?.list
      data.total=res.data?.total
    }else {
      ElMessage.error(res.msg)
    }
  })
}
load()

const handleAdd=()=>{
  data.form={}
  data.formVisible=true
}

const handleEdit=(row)=>{
data.form=JSON.parse(JSON.stringify(row))
  data.formVisible=true
}

const add=()=>{
  request.post('/postsSection/add',data.form).then(res=>{
    if (res.code==='200'){
      ElMessage.success('新增成功')
      data.formVisible=false
      load()
    }else {
      ElMessage.error(res.msg)
    }
  })
}

const update=()=>{
  request.put('/postsSection/update',data.form).then(res=>{
    if (res.code==='200'){
      ElMessage.success('更新成功')
      data.formVisible=false
      load()
    }else {
      ElMessage.error(res.msg)
    }
  })
}

const save=()=>{
formRef.value.validate(valid=>{
  if(valid){
    data.form.id?update():add()
  }
})
}

const del=(id)=>{
  ElMessageBox.confirm('删除后无法回复，确认删除吗？','删除确认',{type:'warning'}).then(res=>{
    request.delete('/postsSection/delete/'+id).then(res=> {
      if (res.code === '200') {
        ElMessage.success('删除成功')
        load()
      } else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(err=>{})
}

const reset=()=>{
  data.title=null
  load()
}

const delBatch=()=>{
  if (!data.ids.length){
    ElMessage.warning("请选择数据")
    return
  }
  ElMessageBox.confirm('删除后无法回复，确认删除吗？','删除确认',{type:'warning'}).then(res=>{
    // 将 ids 参数以查询字符串的形式添加到 URL 中
    const url = `postsSection/delete/batch?ids=${data.ids.join(',')}`;
   request.delete(url).then(res=>{
     if (res.code === '200') {
       ElMessage.success('删除成功')
       load()
     } else {
       ElMessage.error(res.msg)
     }
   })
  }).catch(err=>{
    console.error(err)
  })
}
const handleSelectionChange=(rows)=>{
  data.ids=rows.map(v=>v.id)
}


</script>